# resize images in dir
# using tf resize, normal, keep input consistent.
import os
import cv2
import argparse


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    # in dir, out dir, size_w, size_h
    parser.add_argument('--in-dir',
                        type=str,
                        help="images dir which need resize")
    parser.add_argument('--out-dir',
                        type=str,
                        help="images dir which used to save resize images.") 
    parser.add_argument('--w',
                        type=str,
                        help="new width")
    parser.add_argument('--h',
                        type=str,
                        help="new height")

    args = parser.parse_args()
    in_dir = args.in_dir
    out_dir = args.out_dir
    w = int(args.w)
    h = int(args.h)

    files = os.listdir(in_dir)
    for f in files:
        if '.jpg' in f.lower() or '.jpeg' in f.lower() or '.bpm' in f.lower() or '.png' in f.lower():
            # resize image
            img = cv2.imread(os.path.join(in_dir, f))
            img = cv2.resize(img, (w, h), interpolation=cv2.INTER_CUBIC)
            f_pre = os.path.splitext(f)[0]
            if '.jpg' in f_pre:
                f_pre = f_pre.replace('.jpg', '')
            f = f_pre + '.jpg'
            cv2.imwrite(os.path.join(out_dir, f), img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])

